﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.HVAC
{
	public partial class FormPrefixSetting : System.Windows.Forms.Form
	{
		public FormPrefixSetting()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void FormPrefixSetting_Load(object sender, EventArgs e)
		{
			this.userSetting = new UserSetting();
			this.allPrefixs = this.userSetting.GetAllPrefixSetting();
			if (this.allPrefixs == null)
			{
				return;
			}
			int num = -1;
			foreach (PrefixSetting prefixSetting in this.allPrefixs)
			{
				num = this.dgvShow.Rows.Add();
				this.beCheck = false;
				this.dgvShow.Rows[num].Cells["ColPrefix"].Value = prefixSetting.PrefixValue;
				this.beCheck = true;
				this.dgvShow.Rows[num].Cells["ColMaterial"].Value = prefixSetting.MaterialName;
			}
			if (num > -1)
			{
				DataGridViewRow row = this.dgvShow.Rows[num];
				this.EditAndSelectCell("ColMaterial", row);
			}
		}

		private void SetRowsReadOnly()
		{
			foreach (object obj in ((IEnumerable)this.dgvShow.Rows))
			{
				((DataGridViewRow)obj).Cells["ColMaterial"].ReadOnly = true;
			}
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			if (this.isExist)
			{
				YJKMessageBox.Warning("管材名称不能重复！");
				if (this.reRowIndex > -1)
				{
					DataGridViewRow row = this.dgvShow.Rows[this.reRowIndex];
					this.EditAndSelectCell("ColMaterial", row);
				}
				return;
			}
			int num = this.dgvShow.Rows.Add();
			this.beCheck = false;
			this.dgvShow.Rows[num].Cells["ColPrefix"].Value = "DN";
			this.beCheck = true;
			this.dgvShow.Rows[num].Cells["ColMaterial"].Value = "管道材质";
			if (num > -1)
			{
				DataGridViewRow row2 = this.dgvShow.Rows[num];
				this.EditAndSelectCell("ColMaterial", row2);
			}
		}

		private void btnSure_Click(object sender, EventArgs e)
		{
			try
			{
				string text = string.Empty;
				foreach (object obj in ((IEnumerable)this.dgvShow.Rows))
				{
					DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
					if (dataGridViewRow.Cells["ColMaterial"].Value == null)
					{
						YJKMessageBox.Warning("管材不能为空！");
						this.EditAndSelectCell("ColMaterial", dataGridViewRow);
						return;
					}
					if (dataGridViewRow.Cells["ColPrefix"].Value == null)
					{
						YJKMessageBox.Warning("前缀不能为空！");
						this.EditAndSelectCell("ColPrefix", dataGridViewRow);
						return;
					}
					if (this.IsExistName(dataGridViewRow.Cells["ColMaterial"].Value.ToString()))
					{
						text = dataGridViewRow.Cells["ColMaterial"].Value.ToString();
						break;
					}
				}
				if (!string.IsNullOrEmpty(text))
				{
					DataGridViewRow dataGridViewRow2 = this.FindLastMatch(text);
					YJKMessageBox.Warning("管材名称不能重复！");
					this.EditAndSelectCell("ColMaterial", dataGridViewRow2);
					this.reRowIndex = dataGridViewRow2.Index;
				}
				else
				{
					this.userSetting.WriteAllPrefixSetting(this.GetActiveViewInfo());
					YJKMessageBox.Information("保存或修改成功！");
					base.Close();
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw;
			}
		}

		private DataGridViewRow FindLastMatch(string reName)
		{
			DataGridViewRow result = null;
			string empty = string.Empty;
			for (int i = this.dgvShow.Rows.Count - 1; i >= 0; i--)
			{
				if (this.dgvShow.Rows[i].Cells["ColMaterial"].Value.ToString().Equals(reName))
				{
					result = this.dgvShow.Rows[i];
					break;
				}
			}
			return result;
		}

		private void EditAndSelectCell(string columnName, DataGridViewRow row)
		{
			if (row == null)
			{
				return;
			}
			this.dgvShow.ClearSelection();
			this.dgvShow.CurrentCell = row.Cells[columnName];
			this.dgvShow.BeginEdit(true);
		}

		private List<PrefixSetting> GetActiveViewInfo()
		{
			List<PrefixSetting> list = new List<PrefixSetting>();
			foreach (object obj in ((IEnumerable)this.dgvShow.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				list.Add(new PrefixSetting
				{
					MaterialName = dataGridViewRow.Cells["ColMaterial"].Value.ToString(),
					PrefixValue = dataGridViewRow.Cells["ColPrefix"].Value.ToString()
				});
			}
			return list;
		}

		private void EditAndSelectCell(int rowIndex, int columnIndex)
		{
			this.dgvShow.ClearSelection();
			this.dgvShow.Rows[rowIndex].Cells[columnIndex].Selected = true;
			this.dgvShow.CurrentCell = this.dgvShow.Rows[rowIndex].Cells[columnIndex];
			this.dgvShow.BeginEdit(true);
		}

		private void btnCancel_MouseHover(object sender, EventArgs e)
		{
		}

		private void btnCancel_MouseLeave(object sender, EventArgs e)
		{
		}

		private bool IsExistName(string name)
		{
			if (this.dgvShow.Rows.Count <= 0)
			{
				return false;
			}
			List<string> list = new List<string>();
			for (int i = 0; i < this.dgvShow.Rows.Count; i++)
			{
				object value = this.dgvShow.Rows[i].Cells["ColMaterial"].Value;
				if (value != null)
				{
					list.Add(value.ToString());
				}
			}
			return (from x in list
			where x.Equals(name)
			select x).ToList<string>().Count == 2;
		}

		private void dgvShow_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
			if (!this.beCheck)
			{
				return;
			}
			if (e.RowIndex < 0 || e.ColumnIndex < 0)
			{
				return;
			}
			try
			{
				object value = this.dgvShow.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
				if (this.dgvShow.Columns[e.ColumnIndex].Name == "ColPrefix" && value == null)
				{
					YJKMessageBox.Warning("前缀不能为空！");
					this.EditAndSelectCell(e.RowIndex, e.ColumnIndex);
				}
				else if (this.dgvShow.Columns[e.ColumnIndex].Name == "ColMaterial" && value == null)
				{
					YJKMessageBox.Warning("管材不能为空！");
					this.EditAndSelectCell(e.RowIndex, e.ColumnIndex);
				}
				else
				{
					string text = value.ToString();
					if (this.dgvShow.Columns[e.ColumnIndex].Name == "ColMaterial" && !string.IsNullOrEmpty(text))
					{
						this.isExist = this.IsExistName(text);
					}
					if (this.isExist)
					{
						YJKMessageBox.Warning("管材名称不能重复！");
						this.EditAndSelectCell(e.RowIndex, e.ColumnIndex);
						this.reRowIndex = e.RowIndex;
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				YJKMessageBox.Warning("填写内容有误！");
				this.EditAndSelectCell(e.RowIndex, e.ColumnIndex);
			}
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			int num = 256;
			int num2 = 260;
			if ((msg.Msg == num | msg.Msg == num2) && keyData == Keys.Escape)
			{
				base.Close();
			}
			return false;
		}

		private UserSetting userSetting;

		private bool beCheck;

		private int reRowIndex = -1;

		private bool isExist;

		private List<PrefixSetting> allPrefixs = new List<PrefixSetting>();
	}
}
